<?php

namespace App\Admin\Controllers;

use App\Good;
use App\HomePage;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class HomePageController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('首页设置');
            $content->description('首页设置');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('首页编辑');
            $content->description('首页编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新增设置');
            $content->description('新增设置');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(HomePage::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->img('缩略图')->display(function ($thumb) {
                return "<img src='$thumb' width='100px'>";
            });
            $grid->good()->goods_name('绑定商品名称');
            $grid->sort('排序(从大到小展示)')->sortable();
            $grid->disableExport();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(HomePage::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->image('img','首页展示图片')->help('尽量上传640*345比例的图片，图片将自动压缩成此比例')->rules('required',[
                'required'=>'请上传展示图片'
            ]);
            $form->select('goods_id','绑定商品')->options(Good::getSelectOptions());
            $form->number('sort','排序，数字越大 展示越靠前');
        });
    }
}
